//
//  JFChartLoadingView.m
//  SLAppProduct
//
//  Created by Jianbo Cai on 2018/4/16.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "JFChartLoadingView.h"
#import "JFKitPri.h"

@interface JFChartLoadingView()
{
    UIActivityIndicatorView *_indicatorView;
    UILabel *_desLabel;
    UIButton *_refreshBtn;
    UIImageView *_refreshIconImageView;
}

@end


@implementation JFChartLoadingView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self customUI];
    }
    return self;
}

- (void)customUI{
    
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
//    self.layer.borderWidth = 0.5;
//    self.layer.borderColor = [UIColor awColorWithHexString:@"#dddddd"].CGColor;
    
    _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _indicatorView.backgroundColor = [UIColor clearColor];
    _indicatorView.color = [UIColor redColor];//ThemeColor(KSLRedColorKey);
    
    _indicatorView.center = self.center;
    [self addSubview:_indicatorView];
    [_indicatorView setHidesWhenStopped:YES];
    
    
    _desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 3)];
    _desLabel.font = [UIFont systemFontOfSize:14.0];
    _desLabel.textColor = AWThemeColorForKey(@"Global.nameColor");
    [self addSubview:_desLabel];
    
    
    UILabel *refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    refreshLabel.font = [UIFont systemFontOfSize:14.0];
    refreshLabel.textColor = [UIColor awColorWithHexString:@"#24a9fe"];
    refreshLabel.text = @"点击刷新";
    [refreshLabel sizeToFit];
    
    
    _refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_refreshBtn setFrame:refreshLabel.frame];
    [_refreshBtn addTarget:self action:@selector(refreshAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_refreshBtn];
    
    [_refreshBtn addSubview:refreshLabel];
    _refreshBtn.hidden = YES;
    
    _refreshIconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
    _refreshIconImageView.image = [UIImage imageNameFromBuddle:@"refreshIcon" buddleName:@"JFKitResource"];
    [self addSubview:_refreshIconImageView];
    _refreshIconImageView.hidden = YES;
    
}

- (void)refreshAction:(UIButton *)sender{
    if (self.loadingViewRefreshHandle) {
        _loadingViewRefreshHandle();
    }
}


- (void)setLoadingState:(SLLoadingState)state StateDescrib:(NSString *)des{
    
    _desLabel.text = des;
    [_desLabel sizeToFit];
    _desLabel.center = CGPointMake(self.frameWidth/2, self.frameHeight/2);
    _indicatorView.center = CGPointMake(_desLabel.frameX-20, self.frameHeight/2);
    
    _refreshIconImageView.hidden = YES;
    _refreshBtn.hidden = YES;
    _refreshBtn.center = CGPointMake(self.frameWidth/2, self.frameHeight/2);;
    _refreshIconImageView.center = CGPointMake(self.frameWidth/2, self.frameHeight/2);;
    
    if (state==SLLoadingStateBegin) {
        [_indicatorView startAnimating];
        
    }
    else if (state==SLLoadingStateFinish){
        [_indicatorView stopAnimating];
        
    }
    else if (state==SLLoadingStateError){
        [_indicatorView stopAnimating];
        
        _refreshBtn.hidden = NO;
        _refreshIconImageView.hidden = NO;
        
        _desLabel.center = CGPointMake(_desLabel.center.x-_refreshBtn.frameWidth/2, _desLabel.center.y);
        [_refreshBtn setFrameX:CGRectGetMaxX(_desLabel.frame)];
        [_refreshIconImageView setFrameX:CGRectGetMinX(_desLabel.frame)-_refreshIconImageView.frameWidth-2];
    }
    
}


@end
